package com.example.learnflagservice.controller;

import com.example.learnflagservice.entity.chapter_completion;
import com.example.learnflagservice.service.chapterCompService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/completion")
public class completionController {
    
    @Autowired
    private chapterCompService chapterCompService;
    
    // 标记章节为已完成
    @PostMapping("/complete")
    public String completeChapter(@RequestBody Map<String, Object> requestBody) {
        try {
            int userId = (int) requestBody.get("userId");
            int courseId = (int) requestBody.get("courseId");
            int chapterId = (int) requestBody.get("chapterId");
            
            // 检查是否已经完成过
            chapter_completion existing = chapterCompService.lambdaQuery()
                    .eq(chapter_completion::getUser_id, userId)
                    .eq(chapter_completion::getChapter_id, chapterId)
                    .one();
            
            if (existing != null) {
                return "该章节已经完成过了";
            }
            
            // 创建新的完成记录
            chapter_completion completion = new chapter_completion();
            completion.setUser_id(userId);
            completion.setCourse_id(courseId);
            completion.setChapter_id(chapterId);
            completion.setCompleted_flag(1);
            
            // 设置完成时间
            String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            completion.setCompleted_at(currentTime);
            
            chapterCompService.save(completion);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "标记完成失败: " + e.getMessage();
        }
    }
    
    // 获取用户的章节完成情况
    @GetMapping("/user/{userId}/course/{courseId}")
    public List<chapter_completion> getUserChapterCompletions(@PathVariable int userId, @PathVariable int courseId) {
        return chapterCompService.lambdaQuery()
                .eq(chapter_completion::getUser_id, userId)
                .eq(chapter_completion::getCourse_id, courseId)
                .list();
    }
    
    // 检查特定章节是否已完成
    @GetMapping("/user/{userId}/chapter/{chapterId}")
    public Map<String, Object> checkChapterCompletion(@PathVariable int userId, @PathVariable int chapterId) {
        Map<String, Object> result = new HashMap<>();
        
        chapter_completion completion = chapterCompService.lambdaQuery()
                .eq(chapter_completion::getUser_id, userId)
                .eq(chapter_completion::getChapter_id, chapterId)
                .one();
        
        if (completion != null) {
            result.put("completed", true);
            result.put("completedAt", completion.getCompleted_at());
        } else {
            result.put("completed", false);
        }
        
        return result;
    }
    
    // 取消章节完成状态
    @DeleteMapping("/user/{userId}/chapter/{chapterId}")
    public String cancelChapterCompletion(@PathVariable int userId, @PathVariable int chapterId) {
        try {
            chapterCompService.lambdaUpdate()
                    .eq(chapter_completion::getUser_id, userId)
                    .eq(chapter_completion::getChapter_id, chapterId)
                    .remove();
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "取消完成失败: " + e.getMessage();
        }
    }
}
